#! /bin/sh

#install -d pool

APTDIR="/var/cache/apt/share-apt"
editor="emacs"

if [ "$1" == "help" ] ; then
    echo "usage: share-apt.sh [pwd] [emacs|vim|vi] [add] [edit] [update] [apache] [help]"
    echo "add	(add information to source list automatically)"
    echo "edit	(edit source list)"
    echo "update  (update source list than execute apt-get update)"
    echo "pwd	(use current directory instead of $APTDIR,need to be the first argument)"
    exit 0
fi

if [ "$1" == "pwd" ] ; then
    APTDIR="`pwd`"
fi

if [ ! -e "$APTDIR" ] ; then
    mkdir -p "$APTDIR"
    if [ $? -eq 1 ] ; then
	echo "程序出错，可能因为权限不够，请使用sudo命令或root用户运行该程序"
	exit 1
    fi
fi

cd "$APTDIR"
touch .auth
if [ $? -eq 1 ] ; then
    echo "程序出错，可能因为权限不够，请使用sudo命令或root用户运行该程序"
    exit 1
fi

cd /var/cache/apt/archives
sudo deal-apt-cache.sh
if [ $? -eq 1 ]; then
    echo "deal-apt-cache.sh error"
    exit 1
fi

cd -
rm -Rf dists
rm -f pool
ln -s /var/cache/apt/archives pool
sudo chmod 777 -R pool/
install -d dists/unstable/main/binary-i386
install -d dists/unstable/main/source

ls -1 pool | sed 's/_.*$/ priority section/' | uniq > .override

if [ -e pool/partial ] ; then
    cp -R pool/partial/ ./
    rm -Rf pool/partial
fi
dpkg-scanpackages pool .override 2>.error.log | tee dists/unstable/main/binary-i386/Packages
dpkg-scansources pool | tee dists/unstable/main/source/Sources
if [ -e partial/ ] ; then
    mv partial/ pool/
    sudo chmod -R 777 pool/partial
fi
cat > dists/unstable/main/binary-i386/Release << EOF
Archive: unstable
Version: 1.0
Component: main
Origin: Local
Label: Local
Architecture: i386
EOF

while [ $# -ne 0 ]
do
    if [ "$1" == "emacs" ] ; then
	editor="emacs -nw"
    fi
    if [ "$1" == "vim" ] ; then
	editor="vim"
    fi
    if [ "$1" == "vi" ] ; then
	editor="vi"
    fi
    if [ "$1" == "add" ] ; then
     echo "deb file:`pwd` unstable main" > sources.list
     echo "deb-src file:`pwd` unstable main" >> sources.list
     cat /etc/apt/sources.list >> sources.list
     sudo cp /etc/apt/sources.list /etc/apt/sources.list.old
     sudo cp sources.list /etc/apt/sources.list
    fi
    if [ "$1" == "edit" ] ; then
	echo "参考 APT URL"
	echo
	echo "deb http://$(/sbin/ifconfig | grep "inet addr" | awk '{ print $2 }' | sed 's/^addr:\(.*\)/\1/' | grep -v "^127")/apt unstable main"
	echo "deb-src http://$(/sbin/ifconfig | grep "inet addr" | awk '{ print $2 }' | sed 's/^addr:\(.*\)/\1/' | grep -v "^127")/apt unstable main"
	echo
	echo "将编辑/etc/apt/sources.list,回车确认"
	read input
	sudo "$editor" /etc/apt/sources.list
    fi
    if [ "$1" == "apache" ] ; then
	echo "参考apache配置"
	echo 
	echo "	Alias /apt "`pwd`""
	echo "	<Directory \"`pwd`\">"
        echo "		Options Indexes MultiViews FollowSymLinks"
        echo "		AllowOverride None"
        echo "		Order deny,allow"
        echo "		Allow from 127.0.0.0/255.0.0.0 ::1/128"
	echo "	</Directory>"
	echo
	echo "将编辑apache配置文件,回车确认"
      	read input
	sudo "$editor" /etc/apache2/sites-available/default
	sudo /etc/init.d/apache2 restart
    fi
    if [ "$1" == "update" ] ; then
	sudo apt-get update
    fi
    shift
done

if [ -e .error.log ] ; then
    mkdir -p old
    grep "used that one and ignored data from" .error.log | sed 's/^ *used that one and ignored data from \(.*\) !/\1/g' > .error.log2
    grep "^ ! Package .* is repeat;" .error.log | sed 's/^.*(filename \(.*\)) is repeat;/\1/g' >> .error.log2
    while read line
    do
	mv "$line" old/
    done < .error.log2
    rm .error.log2 .error.log
fi